import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../config/public_text.dart';
import '../../foundation/style/bold_text_style.dart';

/// FileName action_sheet_header
///
/// @Author lax
/// @Date 2023/6/28
///
/// @Description

class ActionSheetHeader extends StatelessWidget {
  final String? title;

  final TextStyle? titleStyle;

  final Widget? titleWidget;

  final String? cancelText;

  final String? confirmText;

  final TextStyle? cancelTextStyle;

  final TextStyle? confirmTextStyle;

  /// 设置后左侧显示取消按钮
  final VoidCallback? onCancel;

  /// 设置后右侧显示确认按钮
  final VoidCallback? onConfirm;

  /// 设置后右侧显示关闭按钮
  final VoidCallback? onClose;

  /// 设置后右侧显示rightButton
  final Widget? rightButton;

  final Color? color;

  final int? maxLines;

  const ActionSheetHeader({
    super.key,
    this.title,
    this.titleStyle,
    this.titleWidget,
    this.cancelText,
    this.confirmText,
    this.cancelTextStyle,
    this.confirmTextStyle,
    this.onCancel,
    this.onConfirm,
    this.onClose,
    this.rightButton,
    this.color,
    this.maxLines = 1,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          onCancel == null
              ? Visibility(
                  visible: false,
                  maintainSize: true,
                  maintainState: true,
                  maintainAnimation: true,
                  maintainInteractivity: false,
                  child: button(),
                )
              : Visibility(
                  child: cancelButton(),
                ),
          Expanded(
            child: titleWidget ??
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  child: Text(
                    title ?? '',
                    maxLines: maxLines,
                    overflow: maxLines == null ? null : TextOverflow.ellipsis,
                    textAlign: TextAlign.center,
                    style: titleStyle ?? BoldTextStyle(),
                  ),
                ),
          ),
          button(),
        ],
      ),
    );
  }

  Widget button() {
    return onConfirm != null
        ? confirmButton()
        : onClose != null
            ? closeButton()
            : (rightButton ?? const SizedBox());
  }

  Widget cancelButton() {
    return InkWell(
      onTap: () {
        if (onCancel != null) {
          onCancel!();
        }
      },
      child: Container(
        color: Colors.transparent,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        child: Text(
          cancelText ?? PublicText.cancel,
          style:
              cancelTextStyle ?? BoldTextStyle(color: PublicColor.grayText),
        ),
      ),
    );
  }

  Widget confirmButton() {
    return InkWell(
      onTap: () {
        if (onConfirm != null) {
          onConfirm!();
        }
      },
      child: Container(
        color: Colors.transparent,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        child: Text(
          confirmText ?? PublicText.confirm,
          style:
              confirmTextStyle ?? BoldTextStyle(color: PublicColor.primary),
        ),
      ),
    );
  }

  Widget closeButton() {
    return InkWell(
      onTap: () {
        if (onClose != null) {
          onClose!();
        }
      },
      child: Container(
        color: Colors.transparent,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
        child: Icon(
          Icons.close_outlined,
          color: PublicColor.grayText,
          size: 20,
        ),
      ),
    );
  }
}
